package br.com.intelsys.iflow.driver.struts;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

class CfgTransition {
	private List <String> nameList;
	private List <CfgHandle> handleList;
	private TransitionValidator transitionValidator;
	private static final Log log = LogFactory.getLog(CfgTransition.class);
	
	CfgTransition (String name) {
		String[] nameArr = new String[1];
		nameArr[0] = name;
		setNameList(nameArr);
	}

	CfgTransition (String[] nameList) {
		this.setNameList(nameList);
	}

	List <String> getNameList() {
		return (this.nameList);
	}
	
	boolean containsName(String name) {
		return (nameList != null && this.nameList.contains(name));
	}
	
	private void setNameList(String[] nameList) {
		if (nameList != null) {
			if (this.nameList == null) {
				this.nameList = new ArrayList <String> (nameList.length);
			}
			for (int i=0;i<nameList.length;i++) {
				this.nameList.add(nameList[i]);
			}
		}
	}
	
	List<CfgHandle> getHandleList() {
		return (handleList);
	}
	void addHandle(CfgHandle handle) {
		if (this.handleList == null) {
			this.handleList = new ArrayList <CfgHandle>();
		}
		this.handleList.add(handle);
	}
	
	@SuppressWarnings("unchecked")
	void setTransitionValidatorClassName(String className) {
		 try {
			Class <TransitionValidator> c = (Class<TransitionValidator>) Class.forName(className);
			this.transitionValidator = (TransitionValidator) c.newInstance();
		} catch (ClassNotFoundException e) {
			log.error("Nao foi possivel encontrar a classe: "+className);
			e.printStackTrace();
		} catch (InstantiationException e) {
			log.error("Nao foi possivel criar uma instancia da classe: "+className);
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			log.error("Nao foi possivel acesso para criar uma instancia da classe: "+className);
			e.printStackTrace();
		} catch (ClassCastException e) {
			log.error("Nao foi possivel converter para TransitionValidator usando a classe: "+className);
			e.printStackTrace();
		}
	}

	TransitionValidator getTransitionValidator() {
		return(this.transitionValidator);
	}
}
